package com.example.boot3.controller;

import com.alibaba.excel.EasyExcel;
import com.example.boot3.model.Vehicle;
import com.example.boot3.service.VehicleService;
import com.example.boot3.util.JsonData;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;


/**
 * @author 邹成明
 */
@CrossOrigin(origins =  "*")
@RestController

@RequestMapping("/home")
public class HomeController {

    @Autowired
    private VehicleService vehicleService;

    @GetMapping("export")
    public void export(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Disposition", "attachment; filename=vehicles.xlsx");
        List<Vehicle> data = vehicleService.getVehiclesInfoList();
        EasyExcel.write(response.getOutputStream(), Vehicle.class).sheet("车辆信息").doWrite(data);
//        return JsonData.buildSuccess("");
    }



    @GetMapping("test")
    public String Test(String[] args) {
        return "test";

    }

}
